package com.chinaisafe.user.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaisafe.base.mybatisplus.util.MpQueryWrapperPageUtil;
import com.chinaisafe.gateway.sdk.enums.ClientTypeEnum;
import com.chinaisafe.user.biz.SysTenantBiz;
import com.chinaisafe.user.entity.SysTenantApp;
import com.chinaisafe.user.mapper.SysTenantAppMapper;
import com.chinaisafe.user.service.SysTenantAppService;
import com.chinaisafe.user.vo.request.SysTenantAppReqVO;
import com.chinaisafe.user.vo.request.TenantAppAuthReq;
import com.chinaisafe.user.vo.response.SysTenantAppRespVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * SysTenantBizImpl
 * Description：
 * 租户管理
 *
 * @author tby
 * &#064;date  2024/7/13 13:53
 */
@Service
public class SysTenantBizImpl implements SysTenantBiz {
    @Resource
    private SysTenantAppService sysTenantAppService;

    @Resource
    private SysTenantAppMapper sysTenantAppMapper;


    @Override
    public void appAuthorize(TenantAppAuthReq tenantAppAuthReq) {
        List<SysTenantApp> tenantApps = sysTenantAppService.lambdaQuery().eq(SysTenantApp::getTenancyCode, tenantAppAuthReq.getTenancyCode()).list();
        Map<Long, SysTenantApp> tenantAppMap = tenantApps.stream().collect(Collectors.toMap(SysTenantApp::getId, Function.identity()));
        //删除取消授权的应用
        List<TenantAppAuthReq.AppAuth> apps = tenantAppAuthReq.getApps();

        sysTenantAppService.lambdaUpdate().set(SysTenantApp::getDeleted, true)
                .eq(SysTenantApp::getTenancyCode, tenantAppAuthReq.getTenancyCode())
                .notIn(CollectionUtil.isNotEmpty(apps),SysTenantApp::getSysAppId, apps.stream().map(TenantAppAuthReq.AppAuth::getSysAppId).collect(Collectors.toList()))
                .update();

        if (CollectionUtil.isEmpty(apps)) {
            return;
        }
        List<SysTenantApp> result = new ArrayList<>();
        for (int i = 0; i < apps.size(); i++) {
            TenantAppAuthReq.AppAuth app = apps.get(i);
            SysTenantApp sysTenantApp = tenantAppMap.get(app.getSysAppId());
            if (sysTenantApp == null) {
                // 新增
                sysTenantApp = app.toSysTenantApp(tenantAppAuthReq.getTenancyCode(), i);
            } else {
                sysTenantApp.setSeq(i);
                sysTenantApp.setTenancyDate(app.getTenancyDate());
            }
            result.add(sysTenantApp);

        }
        sysTenantAppService.saveOrUpdateBatch(result);
    }

    @Override
    public Map<ClientTypeEnum, List<SysTenantAppRespVO>> queryList(String tenancyCode) {
        SysTenantAppReqVO sysTenantAppReqVO = new SysTenantAppReqVO();
        sysTenantAppReqVO.setPageSize(1000);
        sysTenantAppReqVO.setTenancyCode(tenancyCode);

        Map<ClientTypeEnum, List<SysTenantAppRespVO>> result = new HashMap<>(ClientTypeEnum.values().length);
        for (ClientTypeEnum clientTypeEnum: ClientTypeEnum.values()){
            // 分页设置
            Page<SysTenantApp> page = MpQueryWrapperPageUtil.buildWrapperPage(new Page<>(), sysTenantAppReqVO);
            IPage<SysTenantAppRespVO> pageList = sysTenantAppMapper.getPageList(page, sysTenantAppReqVO);
            result.put(clientTypeEnum, pageList.getRecords());
        }

        return result;
    }
}
